package com.tongzhou.client;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.initInfo.dao.IInitInfoDAO;
import com.tongzhou.tongzhou.initInfo.dao.impl.InitInfoDAO;
import com.tongzhou.tongzhou.initInfo.model.InitInfo;
import com.tongzhou.tongzhou.initInfo.model.QuotType;
import com.tongzhou.tongzhou.initInfo.model.Region;
import com.tongzhou.util.Util;

//初始化接口
public class ClientInit {
	IInitInfoDAO dao=new InitInfoDAO();
	public String getInfo(String json,HttpServletResponse response){
		JSONObject jsonObject = JSONObject.fromObject( json );
		Integer initNo=null;
		if(jsonObject.get("initNo")!=null){
			initNo=(Integer) jsonObject.get("initNo");
		}
		StringBuffer str=new StringBuffer();
		try {
			InitInfo initInfo=dao.getInitInfoById(initNo);
			if(initInfo==null){
				return "{'success':0,'msg':'输入版本号不存在!'}";
			}else{
				try {
					StringBuffer sendStr=new StringBuffer();
					sendStr.append("http://www.tongzhoudg.com/tongzhouClient/client.do?method=3");
					sendStr.append("&json={'serverCall':'"+initInfo.getServer_call()+"','serverCall':'"+initInfo.getServer_call()+"','serverCall':'"+initInfo.getServer_call()+"'}");
					response.sendRedirect(sendStr.toString());
				} catch (IOException e) {
					e.printStackTrace();
				}
//				str.append("{'success':1,'msg':{'initNo':"+initInfo.getId()+",");
//				str.append("'serverCall':'"+Util.changeToObj(initInfo.getServer_call())+"',");
//				str.append("'offerCall':'"+Util.changeToObj(initInfo.getOffer_call())+"',");
//				str.append("'aboutCall':'"+Util.changeToObj(initInfo.getAbout_call())+"',");
//				str.append("'regionList':[");
//				List<Region> regionList=dao.getRegionList();
//				if(regionList.size()>0){
//					for(int i=0;i<regionList.size()-1;i++){
//						Region r=regionList.get(i);
//						str.append("{'rid':"+r.getId()+",'regionName':'"+Util.changeToObj(r.getRegion_name())+"'},");
//					}
//					Region r=regionList.get(regionList.size()-1);
//					str.append("{'rid':"+r.getId()+",'regionName':'"+Util.changeToObj(r.getRegion_name())+"'}");
//				}
//				str.append("],");
//				str.append("'quotList':[");
//				List<QuotType> quotTypeList=dao.getQuotTypeList();
//				if(quotTypeList.size()>0){
//					for(int i=0;i<quotTypeList.size()-1;i++){
//						QuotType q=quotTypeList.get(i);
//						str.append("{'qid':"+q.getId()+",'quotName':'"+Util.changeToObj(q.getQuot_type_name())+"'},");
//					}
//					QuotType q=quotTypeList.get(quotTypeList.size()-1);
//					str.append("{'qid':"+q.getId()+",'quotName':'"+Util.changeToObj(q.getQuot_type_name())+"'}");
//				}
//				str.append("]}}");
//				return str.toString();
			}
		} catch (DBException e) {
			e.printStackTrace();
		}
		
//		StringBuffer str=new StringBuffer();
//		str.append("{'success':1,'msg':{'initNo':123,'serverCall':'18938035656',");
//		str.append("'offerCall':'18938035657','aboutCall':'18938035658',");
//		str.append("'regionList':[{'rid':1,'regionName':'广州'},{'rid':2,'regionName':'深圳'}],");
//		str.append("'quotList':[{'qid':1,'quotName':'电话'},{'qid':2,'quotName':'QQ'}]");
//		str.append("}}");
		return "{'success':0,'msg':'输入参数有误!'}";
	}
}
